package main

import (
	"fmt"
	"os"
	"todolist/app/model"
	"todolist/database"
	"todolist/router"
	"todolist/setting"
)

const defaultConfFile = "./conf/config.ini"

func main() {

	confFile := defaultConfFile

	if len(os.Args) > 2 {
		fmt.Println("使用了指定配置文件: ", os.Args[1])
		confFile = os.Args[1]
	} else {
		fmt.Println("使用默认配置文件: ./conf/config.ini")
	}

	if err := setting.Init(confFile); err != nil {
		fmt.Println("加载配置文件失败")
		return
	}

	if err := database.InitMysql(setting.Conf.MysqlConfig); err != nil {
		fmt.Println("连接数据库失败")
	}

	defer database.Close()
	database.DB.AutoMigrate(&model.Todo{})

	r := router.SetupRouter()
	if err := r.Run(fmt.Sprintf(":%d", setting.Conf.Port)); err != nil {
		fmt.Printf("server startup failed, err:%v\n", err)
	}

}
